package com.qymerge.manage.data.service.impl;

import com.qymerge.manage.base.dao.IUserWXDao;
import com.qymerge.manage.core.bean.ActionResultRO;
import com.qymerge.manage.core.bean.DataTableRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.utils.ReadExcelUtil;
import com.qymerge.manage.data.dao.IYyueDao;
import com.qymerge.manage.data.dto.YyueDto;
import com.qymerge.manage.data.dto.export.YyueExportDTO;
import com.qymerge.manage.data.ro.YyueRO;
import com.qymerge.manage.data.service.IYyueService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *<p>Title: XFRecordServiceImpl</p>
 *<p>Description: 预约记录</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/19
 *@Time 18:18
 */
@Service
public class YyueServiceImpl implements IYyueService {

    @Autowired
    private IYyueDao yyueMgrDao;

    @Autowired
    private IUserWXDao userWXDao;

    /**
     * 查询
     *
     * @param ro
     * @param pageRequest
     * @return
     */
    @Override
    public DataTableRO list(YyueDto ro, PageRequest pageRequest) {
        List<YyueRO> list = yyueMgrDao.list(ro, pageRequest);

        for (YyueRO yyueRO : list) {
            // 设置用户信息
            long userwxid = yyueRO.getUserwxid();
            if (userwxid > 0) {
                yyueRO.setUser_info(userWXDao.findOne(userwxid));
            }
        }

        DataTableRO dataTableRO = new DataTableRO();
        int allCount = yyueMgrDao.total(ro);
        int pageSize = pageRequest.getSize();
        dataTableRO.setAll_count(allCount);
        dataTableRO.setList(list);
        dataTableRO.setCurrent_page(pageRequest.getPage());
        dataTableRO.setPage_size(pageSize);
        int allPage = (allCount % pageSize == 0 ? allCount / pageSize : allCount / pageSize + 1);
        dataTableRO.setAll_page(allPage);
        return dataTableRO;
    }

    @Override
    public YyueRO detail(Long id) {
        YyueRO yyueRO = yyueMgrDao.findOne(id);
        if(null != yyueRO){
            // 设置用户信息
            long userwxid = yyueRO.getUserwxid();
            if (userwxid > 0) {
                yyueRO.setUser_info(userWXDao.findOne(userwxid));
            }
        }
        return yyueRO;
    }

    @Override
    public ActionResultRO save(YyueRO ro) {
        int rn = 0;
        if (ro.getId() == 0) {
            rn = yyueMgrDao.install(ro);
        } else {
            rn = yyueMgrDao.update(ro);
        }
        return new ActionResultRO(rn, ro.getId() == 0 ? "保存成功" : "修改成功", ro.getId() == 0 ? "保存失败" : "修改失败");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public ActionResultRO delete(Long[] ids) {
        int rn = yyueMgrDao.remove(Arrays.asList(ids));
        return new ActionResultRO(true, "删除" + rn +"成功", "删除" + rn +"失败");
    }

    /**
     * 导出表格
     *
     * @param ro
     * @return
     */
    @Override
    public List<YyueExportDTO> exportData(YyueDto ro) {
        List<YyueExportDTO> list = new ArrayList<YyueExportDTO>();
        YyueExportDTO dto = null;

        List<YyueRO> yyueROS = yyueMgrDao.findAll(ro);
        if (CollectionUtils.isNotEmpty(yyueROS)) {
            for (YyueRO yyueRO : yyueROS) {
                dto = new YyueExportDTO();

                dto.setId(yyueRO.getId());
                dto.setHecode(yyueRO.getHecode());
                dto.setUserwxid(yyueRO.getUserwxid());
                dto.setUsername(yyueRO.getUsername());
                dto.setTel(yyueRO.getTel());
                dto.setYdate(yyueRO.getYdate());
                dto.setYtime(yyueRO.getYtime());
                dto.setHxcode(yyueRO.getHxcode());
                dto.setIshx(yyueRO.getId() == 1 ? "是" : "否");
                dto.setHxdate(yyueRO.getHxdate());
                dto.setCreatedate(yyueRO.getCreatedate());

                list.add(dto);
            }
        }

        return list;
    }

    @Override
    public ActionResultRO importData(MultipartFile multipartFile, int flag) {
        StringBuffer tip = new StringBuffer("导入结果：<br/>");
        ReadExcelUtil readExcelUtil = new ReadExcelUtil();
        List<String[][]> datas = null;
        try {
            datas = readExcelUtil.readByStream(multipartFile.getInputStream(), 2, multipartFile.getOriginalFilename());
            if (org.springframework.util.CollectionUtils.isEmpty(datas)) {
                return new ActionResultRO(false, "execl数据为空");
            }
            String[][] data = datas.get(0);

            long id= 0L;
            long userwxid= 0L;
            String hecode = null;
            String username = null;
            String tel= null;
            String ydate= null;
            String ytime= null;
            String hxcode= null;
            int ishx = 0;
            String hxdate = null;
            String createdate = null;

            YyueRO yyueRO = null;
            int rowNum = 1;        // 记录当前行
            int successNum = 0;    // 记录新增或者更新记录数
            for (String[] row : data) {
                rowNum++;

                id = Long.valueOf(row[0]);
                userwxid = Long.valueOf(row[1]);
                hecode = row[2];
                username = row[3];
                tel = row[4];
                ydate= row[5];
                ytime= row[6];
                hxcode= row[7];
                if (StringUtils.equals("是",row[7]) ){
                    ishx = 1;
                } else {
                    ishx = 0;
                }
                hxdate= row[9];
                createdate= row[10];


                if (StringUtils.isEmpty(id + "")
                        || StringUtils.isEmpty(ydate)) {
                    tip.append("第").append(rowNum).append("行：部分数据为空，不予新增<br/>");
                    continue;
                }

                yyueRO = new YyueRO(id, userwxid, hecode, username, tel, ydate, ytime, hxcode, ishx, hxdate, createdate);

                if (flag == 1) {
                    yyueMgrDao.install(yyueRO);
                    successNum++;
                } else if (flag == 0) {
                    yyueMgrDao.update(yyueRO);
                    successNum++;
                }

            }
            tip.append("成功"+ (flag == 1 ? "新增":"更新") +"记录：").append(successNum).append("条");
        } catch (IOException e) {
            e.printStackTrace();
            return new ActionResultRO(false, "导入失败");
        }

        return new ActionResultRO(true, tip.toString());
    }

    @Override
    public ActionResultRO updateIshx(long id, int ishx, String msg) {
        int rn = yyueMgrDao.updateIshx(id, ishx);
        return new ActionResultRO(rn, msg + "成功", msg + "失败");
    }

}
